#define _CRT_SECURE_NO_WARNINGS 1
#include"HeadSort.h"

void test01()
{
	DateType a[] = { 3,5,4,6,8,9,1,6,7,15,45,34,10 };
	int sz = sizeof(a) / sizeof(DateType);
	for (int i = 1; i < sz; i++)
	{
		AdjustUp(a, i);
	}
	for (int i = 0; i < sz; i++)
	{
		printf("%d ", a[i]);
	}
}

void test02()
{
	DateType a[] = { 3,5,4,6,8,9,1,6,7,15,45,34,10 };
	int sz = sizeof(a) / sizeof(DateType);
	for (int i = (sz - 1 - 1) / 2; i >= 0; i--)
	{
		AdjustDown(a, sz, i);
	}
	for (int i = 0; i < sz; i++)
	{
		printf("%d ", a[i]);
	}
}

//创造数据
void CreatDdate()
{
	int N = 10000;
	srand(time(0));
	FILE* pf = fopen("date.txt", "w");
	if (pf == NULL)
	{
		perror("fopen");
		exit(1);
	}
	for(int i=1;i<=N;i++)
	{
		int x = (rand() + i)%10000;
		fprintf(pf, "%d\n", x);
	}
	fclose(pf);
}

void PrintTop_k(int k)
{
	//const char* file = "date.txt";
	FILE* pout = fopen("date.txt", "r");
	if (pout == NULL)
	{
		perror("fopen");
		exit(1);
	}
	DateType* arr = (DateType*)malloc(sizeof(DateType) * k);
	if (arr == NULL)
	{
		perror("malloc");
		exit(1);
	}

	for (int j = 0; j < k; j++)
	{
		fscanf(pout, "%d", &arr[j]);
	}
	
	for (int i = (k - 1 - 1) / 2; i >= 0; i--)
	{
		AdjustDown(arr, k, i);
	}
	int x = 0;;
	while (fscanf(pout, "%d", &x) > 0)
	{
		if (x > arr[0])
		{
			arr[0] = x;
			AdjustDown(arr, k, 0);
		}
	}
	for (int i = 0; i < k; i++)
	{
		printf("%d ", arr[i]);
	}
}
int main()
{
	//test01();
	//test02();
	PrintTop_k(10);
	return 0;
}